package com.cyf.library.util

import android.util.Log
import com.cyf.library.bean.LogBean
import com.cyf.library.thread.ThreadManager

import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddress

const val TAG = "App"

var showLog = true

var clientId: String = "debugClient"

private enum class LEVEL {
    V, D, I, W, E
}


fun String.logv(tag: String = TAG) = log(LEVEL.V, tag, this)
fun String.logd(tag: String = TAG) = log(LEVEL.D, tag, this)
fun String.logi(tag: String = TAG) = log(LEVEL.I, tag, this)
fun String.logw(tag: String = TAG) = log(LEVEL.W, tag, this)
fun String.loge(tag: String = TAG) = log(LEVEL.E, tag, this)

private fun log(level: LEVEL, tag: String, msg: String) {
    if (!showLog) {
        return
    }
    when (level) {
        LEVEL.V -> {
            Log.v(tag, msg)
            upload(tag, msg)
        }
        LEVEL.D -> {
            Log.d(tag, msg)
            upload(tag, msg)
        }
        LEVEL.I -> {
            Log.i(tag, msg)
            upload(tag, msg)
        }
        LEVEL.W -> {
            Log.w(tag, msg)
            upload(tag, msg)
        }
        LEVEL.E -> {
            Log.e(tag, msg)
            upload(tag, msg)
        }
    }
}

private fun upload(tag: String, msg: String) {
    return
    val logBean = LogBean()
    logBean.clientId = clientId
    logBean.log = "$tag-$msg"
    ThreadManager.startThread(Runnable {
        val datagramPacket = DatagramSocket();
        val log = clientId + ":" + tag + "-" + msg
        val p = DatagramPacket(
            log.toByteArray(),
            log.toByteArray().size,
            InetAddress.getByName("139.224.128.240"),
            8888
        )
        datagramPacket.send(p)
    }, "udp-log")

}